package vn.cent.game.cardtype.client;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import vn.cent.game.cardtype.bc.BCBean;
import vn.cent.game.cardtype.beans.GameBean;
import vn.cent.game.cardtype.main.CardTypeGameExtension;
import vn.cent.game.cardtype.utils.GameKind;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class RequestGame extends BaseClientRequestHandler {

	@Override
	public void handleClientRequest(User sender, ISFSObject data) {
		// TODO Auto-generated method stub
		CardTypeGameExtension gameExtension = 
				(CardTypeGameExtension) getParentExtension();
		
		String roomName;
		String roomPassword;
		boolean isQuickPlay;
		
		GameKind gameKind = GameKind.None;
		
		// Get data 
		if (data.containsKey("RoomName"))
		{
			roomName = data.getUtfString("RoomName");
		}
		
		if (data.containsKey("RoomPassword"))
		{
			roomPassword = data.getUtfString("RoomPassword");
		}
		
		byte gKind = data.getByte("GameKind");
		gameKind = SwitchGameKindBaseIndex(gKind);
		
		isQuickPlay = data.getBool("IsQuickGame");
		
		
		ISFSObject dataObj = new SFSObject();
		GameBean avaiableRoom = FindRoom(gameExtension, gameKind);
		
		
		
		if (isQuickPlay)
		{
			/*
			 * User click to quick game request 
			 */
			try
			{
				// Need to check it later
				// make sure the game logic and we can apply user distribute stragety
				getApi().joinRoom(sender, avaiableRoom.getRoomID());
				avaiableRoom.setGuest(sender.getId());
				//avaiableRoom.getPlayers().put(sender.getId(), sender);
			//	avaiableRoom.StartGame(gameExtension, avaiableRoom.getRoomID().getPlayersList());
				avaiableRoom.setStarted(true);
				
			} catch (SFSJoinRoomException jrEx)
			{
				trace("[MakeReady] Join room exception! Please check out below:");
				trace(jrEx.toString());
			}
			
		}
		else // Request to join created room
		{
			
		}
	}
	
	private GameBean FindRoom(CardTypeGameExtension extension, GameKind gameKind)
	{
		/*
		 * Find room stragety:
		 * For each room from this game kind's room list, check if JoinPriority is high
		 * and make join if max_user > current user quantity
		 */
		switch (gameKind) {
		case None:
			{
				// Do nothing right now
				break;
			}
		case BC:
		{
			ConcurrentHashMap<Integer, BCBean> listGameBC = extension.getListGameBC();
			for (Entry<Integer,BCBean> interable_element : listGameBC.entrySet())
			{
				BCBean game = interable_element.getValue();
				if (!game.isStarted() && !game.getRoomID().isPasswordProtected())
				{
					return game;
				}
			}
			break;
		}
			default:
				break;
		}
		
		// Find nothing, return null
		trace("[MakeReady] Find room got somethings wrong. Please check again!");
		return null;
	}
	 
	
	private GameKind SwitchGameKindBaseIndex(byte index)
	{
		
		// BC = 0; CA = 1
		switch (index) {
			case 0:
			{
				return GameKind.BC;
			}
			case 1:
			{
				return GameKind.CA;
			}
		}
		trace("[RequestGame] Switch data got errors. Index = " + index);
		return null;
	}
	
	// Do not use right now. But need to provide new user to room 
	// Do implement later to decrease server traffic load
	// and make room more balance
	private void SortRoomPriority()
	{
		
	}
	
	
}
